home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / odoc / odoc.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.6 KB  |  143 lines

  1. /*
  2.     File:        odoc.c
  3.  
  4.     Contains:    This sample sends an 'odoc' event to a specified running application
  5.  
  6.     Written by: Steven Falkenburg    
  7.  
  8.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 11/16/92                     fixed bug in blockmoving target ID name 
  21.                                             (was moving too much data)
  22. */
  23. #include <Types.h>
  24. #include <PPCToolBox.h>
  25. #include <AppleEvents.h>
  26. #include <Aliases.h>
  27. #include <EPPC.h>
  28. #include <Fonts.h>
  29. #include <Windows.h>
  30. #include <Dialogs.h>
  31. #include <Menus.h>
  32. #include <Fonts.h>
  33. #include <StandardFile.h>
  34.  
  35.  
  36. void main(void);
  37. void InitStuff(void);
  38. OSErr GetTargetAddress(Str255 prompt,Str255 appStr,PortInfoRec *myPortInfo,
  39.                         AEAddressDesc *targetAddress,TargetID *toTargetID);
  40. OSErr SendODOC(AEAddressDesc *targetAddress,AliasHandle targetFile);
  41. Boolean GetFileToOpen(FSSpec *target);
  42.  
  43. void main(void)
  44. {
  45.     PortInfoRec portInfo;
  46.     AEAddressDesc targetAddress;
  47.     TargetID targetID;
  48.     OSErr err;
  49.     FSSpec fileSpec;
  50.     AliasHandle aliasTarget;
  51.     
  52.     InitStuff();
  53.     
  54.     if (!GetFileToOpen(&fileSpec))
  55.         ExitToShell();
  56.     
  57.     if (GetTargetAddress("\pSpecify target of ODOC:",nil,&portInfo,&targetAddress,&targetID)==noErr) {
  58.         err = NewAlias(nil,&fileSpec,&aliasTarget);
  59.         if (err==noErr) {
  60.             err = SendODOC(&targetAddress,aliasTarget);
  61.             err = AEDisposeDesc(&targetAddress);
  62.             DisposeHandle((Handle)aliasTarget);
  63.         }
  64.     }
  65. }
  66.  
  67.  
  68. void InitStuff(void)
  69. {
  70.     InitGraf(&qd.thePort);
  71.     InitFonts();
  72.     InitWindows();
  73.     InitMenus();
  74.     TEInit();
  75.     InitDialogs(nil);
  76.     InitCursor();
  77.     FlushEvents(everyEvent,0);
  78. }
  79.  
  80.  
  81. Boolean GetFileToOpen(FSSpec *target)
  82. {
  83.     StandardFileReply sfReply;
  84.     
  85.     StandardGetFile(nil,-1,nil,&sfReply);
  86.     BlockMove(&sfReply.sfFile,target,sizeof(FSSpec));
  87.     
  88.     return sfReply.sfGood;
  89. }
  90.  
  91.  
  92. OSErr GetTargetAddress(Str255 prompt,Str255 appStr,PortInfoRec *myPortInfo,
  93.                         AEAddressDesc *targetAddress,TargetID *toTargetID)
  94. {
  95.     OSErr err;
  96.     
  97.     err = PPCBrowser(prompt,appStr,false,&toTargetID->location,myPortInfo,nil,"\p");
  98.     if (err==noErr) {
  99.         toTargetID->name = myPortInfo->name;
  100.         err = AECreateDesc(typeTargetID,(Ptr)toTargetID,sizeof(TargetID),targetAddress);
  101.     }
  102.     
  103.     return err;
  104. }
  105.  
  106.  
  107. OSErr SendODOC(AEAddressDesc *targetAddress,AliasHandle targetFile)
  108. {
  109.     OSErr err;
  110.     AppleEvent theAE,reply;
  111.     AEDescList descList;
  112.     
  113.     reply.dataHandle = nil;
  114.     
  115.     err = AECreateAppleEvent(kCoreEventClass,kAEOpenDocuments,targetAddress,
  116.                 kAutoGenerateReturnID,kAnyTransactionID,&theAE);
  117.     if (err!=noErr)
  118.         return err;
  119.     
  120.     err = AECreateList(nil,0,false,&descList);
  121.     if (err!=noErr)
  122.         return err;
  123.     
  124.     HLock((Handle)targetFile);
  125.     err = AEPutPtr(&descList,0,typeAlias,(Ptr)*targetFile,sizeof(AliasHandle)+(**targetFile).aliasSize);
  126.     HUnlock((Handle)targetFile);
  127.     if (err!=noErr)
  128.         return err;
  129.     
  130.     err = AEPutParamDesc(&theAE,keyDirectObject,&descList);
  131.     if (err!=noErr)
  132.         return err;
  133.     
  134.     err = AESend(&theAE,&reply,kAENoReply,kAENormalPriority,kAEDefaultTimeout,nil,nil);
  135.     
  136.     err = AEDisposeDesc(&descList);
  137.     err = AEDisposeDesc(&theAE);
  138.     if (reply.dataHandle)
  139.         err = AEDisposeDesc(&reply);
  140.     return err;
  141. }
  142.  
  143.